#pragma comment(linker, "/STACK:10000000")
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <set>
#include <map>
#include <cstdio>
#include <cassert>
#include <string>
#include <cstring>
#define ldb long double
#define LL long long
#define fi first
#define se second
#define fill(a, c) memset(a, c, sizeof(a))
#define sqr(a) ((a) * (a))
#define nextLine() {int c = 0; while((c = getchar()) != 10 && c != EOF);}
#define getBit(mask, k) (((mask) / pw[k]) % pw[1])
#define setBit(mask, k, l) (((mask) / pw[k + 1] * pw[1] + (l)) * pw[k] + ((mask) % pw[k]))
#define debug(a) cerr << #a << " = " << a << " ";
#define debugl(a) cerr << #a << " = " << a << "\n";
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define ff first
#define ss second 
const ldb LDINF = 9128739847123.00;
const ldb eps = 1e-9;
const int inf = 1 << 28;
const ldb pi = fabsl(atan2l(0.0, -1.0));
using namespace std;

int n, m;
int s[100010], f[100010], t[100010];
vector < pair < int, int > > all;
int res[100010];

LL get_time(LL x, LL k)
{
	if (x == 0 || x == m - 1) 
		return x + k * 2 * (m - 1);
	return x + (k / 2ll) * 2ll * x + (k / 2ll + (k % 2ll)) * 2ll * (m - 1 - x);
}

int dir(LL t)
{
	if ((t / (m - 1)) % 2 == 0) return 1;
	else return -1;
}

void load()
{
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		scanf("%d%d%d", &s[i], &f[i], &t[i]);
		s[i]--;
		f[i]--;
		if (f[i] == s[i])
		{
			printf("%d\n", t[i]);
			continue;
		}
		LL l = 0;
		LL r = 102000000;
		LL mid, res = -1;
		while (l <= r)
		{
			mid = (l + r) / 2;
			if (get_time(s[i], mid) >= t[i])
			{
				res = get_time(s[i], mid);
				r = mid - 1;
			}
			else l = mid + 1;
		}
	//	cerr << i << " " << s[i] << " " << f[i] << " " << res << " " << dir(res) <<"\n";
		if ((f[i] - s[i]) * dir(res) > 0)
			printf("%d\n", (int)res + abs(f[i] - s[i]));
		else printf("%d\n", (int)res + abs(f[i] - s[i]) + (dir(res) < 0 ? 2 * s[i] : 2 * (m - s[i] - 1)));
	}
}

void solve()
{
}

#define file "a"
int main()
{
	#ifndef ONLINE_JUDGE
		freopen(file".in", "rt", stdin);
		freopen(file".out", "wt", stdout);
	#endif
	load();
	solve();
	return 0;
}
